package com.wjm.demo.test1.net_test.udp_socket_test.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Send implements Runnable {
    private DatagramSocket ds;

    public Send(DatagramSocket ds) {
        this.ds = ds;
    }

    @Override
    public void run() {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while ((line = br.readLine()) != null) {
                byte[] bytes = line.getBytes();
//                DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.0.101"), 10000);//只发到101这个IP上，注意101是我的ipv4地址这个错了，服务端是收不到的
                DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.0.255"), ChatDemo.port);//发到255为广播群发，所有人都收得到
                ds.send(dp);
                if ("886".equals(line)) {
                    break;
                }
            }
            ds.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
